package com.service.impl;

import com.mapper.ShopMapper;
import com.pojo.Shop;
import com.pojo.ShopExample;
import com.respository.ShopRepository;
import com.service.ShopService;
import com.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: HL
 * Date: 2020/3/3
 * Time: 14:34
 * Description: No Description
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
  @Autowired
  private ShopRepository shopRepository;
    @Override
    public ServerResponse save(Shop shop) {
       /* Byte status =1;
        shop.setStatus(status);*/
        //执行mybatis添加方法
        int rows = shopMapper.insert(shop);
        //判断添加成功
        if (rows > 0)
            return ServerResponse.addSuccess();
        else
            return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse modifyStatus(Integer id) {
        Shop shop = shopMapper.selectByPrimaryKey(id);
        Byte status = 0;
        shop.setStatus(status);
        int i = shopMapper.updateByPrimaryKeySelective(shop);
        if (i>0)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }

    @Override
    public ServerResponse modify(Shop shop) {
        int i = shopMapper.updateByPrimaryKeySelective(shop);
        if (i>0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse getById(Integer id) {
        Shop shop = shopMapper.selectByPrimaryKey(id);
        return ServerResponse.getDataSuccess(shop);
    }

    @Override
    public ServerResponse getAll() {
        ShopExample example=new ShopExample();
        ShopExample.Criteria criteria = example.createCriteria();
        Byte status = 1;
        criteria.andStatusEqualTo(status);
        List<Shop> shops = shopMapper.selectByExample(example);
        if (shops!=null&&shops.size()>0)
            return ServerResponse.getDataSuccess(shops);
        return ServerResponse.getDataFailed("没有数据");
    }
    /*@Override
    public ServerResponse getBySearch(String kw) {
        System.out.println("service kw:"+kw);
        //构建多条件查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (kw != null && !"".equals(kw.trim())){
            System.out.println("1111");
            //定义需要查询匹配字段
            String [] args = {"shop_name","divide_name","food_type","city","fuzzy_address","detail_address","recommended_dishes","atmophere"};
            //搜索框构建多字段查询条件
            MultiMatchQueryBuilder searchBuilder = QueryBuilders.multiMatchQuery(kw, args);
            System.out.println(searchBuilder);
            //合并多字段查询
            boolQueryBuilder.must(searchBuilder);
            //spring提供的一个查询条件构造器，帮助构建json格式的请求体
            NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
            //查询条件
            builder.withQuery(searchBuilder);
            //生产nativeSearchQuery
            NativeSearchQuery build = builder.build();
            System.out.println(build);
            //调用shopRepository进行查询
            Page<Shop> shopPage = shopRepository.search(build);
            // 总条数
            long total = shopPage.getTotalElements();
            System.out.println("total = " + total);
            for (Shop shop : shopPage) {
                System.out.println(shop);
            }
            return ServerResponse.getDataSuccess(shopPage);
        }
        return ServerResponse.getDataFailed("没有对应的店铺");
    }*/
}
